var gulp = require('gulp'),
    usemin = require('gulp-usemin'),
    wrap = require('gulp-wrap'),
    connect = require('gulp-connect'),
    watch = require('gulp-watch'),
    minifyCss = require('gulp-cssnano'),
    minifyJs = require('gulp-uglify'),
    concat = require('gulp-concat'),
    less = require('gulp-less'),
    rename = require('gulp-rename'),
    minifyHTML = require('gulp-htmlmin'),
    clean = require('gulp-clean');

var paths = {
    scripts: 'src/js/**/*.*',
    styles: 'src/css/**/*.*',
    images: 'src/img/**/*.*',
    templates: 'src/templates/**/*.html',
    index: 'src/index.html',
    login: 'src/login.html',
    bower_fonts: 'src/components/**/*.{ttf,woff,woff2,eof,svg}',
    lib: 'src/lib/**/*.*'
};

gulp.task('usemin', ['usemin-index', 'usemin-login']);

// 打包index.html
gulp.task('usemin-index', function () {
    return gulp.src(paths.index)
        .pipe(usemin({
            js: [minifyJs(), 'concat'],
            css: [minifyCss({keepSpecialComments: 0}), 'concat'],
        }))
        .pipe(gulp.dest('dist/'));
});

// 复制login.html
gulp.task('usemin-login', function () {
    return gulp.src(paths.login)
        .pipe(gulp.dest('dist/'));
});

// 复制资源
gulp.task('build-assets', ['copy-bower_fonts', 'copy-special_fonts', 'copy-lib']);

// 复制字体
gulp.task('copy-bower_fonts', function () {
    return gulp.src(paths.bower_fonts)
        .pipe(rename({
            dirname: '/fonts'
        }))
        .pipe(gulp.dest('dist/lib'));
});

// 复制特殊字体
gulp.task('copy-special_fonts', function () {
    gulp.src('src/components/jsoneditor/dist/img/*.*')
        .pipe(gulp.dest('dist/lib/css/img'));
});

// 复制自定义库
gulp.task('copy-lib', function () {
    gulp.src(paths.lib)
        .pipe(gulp.dest('dist/lib'));
});

// 自定义资源打包
gulp.task('build-custom', ['custom-images', 'custom-js', 'custom-css', 'custom-templates']);

// 复制图片
gulp.task('custom-images', function () {
    return gulp.src(paths.images)
        .pipe(gulp.dest('dist/img'));
});

// 打包js
gulp.task('custom-js', function () {
    return gulp.src(paths.scripts)
        .pipe(minifyJs())
        .pipe(concat('dashboard.min.js'))
        .pipe(gulp.dest('dist/js'));
});

// 打包css
gulp.task('custom-css', function () {
    return gulp.src(paths.styles)
        .pipe(minifyCss())
        .pipe(concat('app.css'))
        .pipe(gulp.dest('dist/css'));
});

// 打包templates
gulp.task('custom-templates', function () {
    return gulp.src(paths.templates)
        .pipe(minifyHTML())
        .pipe(gulp.dest('dist/templates'));
});


/**
 * ************** 热部署（live reload）**************
 * 系统默认监听8888端口，
 * if 只开发前端，则直接访问localhost:8888
 * if 联调后端，则推荐使用nginx做路由转发｛
 *      --此时nginx中监听的端口不可以是8888，
 *      --此时后端监听的端口不可以是8888，
 *      --此时访问页面的端口必定不是8888，
 *      --此时修改文件以后需要手动在页面F5，
 * ｝
 * >>>>>>> 就算不起8888端口监听，联调后端也必须手动Refresh
 */
gulp.task('watch', function () {
    gulp.watch([paths.images], ['custom-images']);
    gulp.watch([paths.scripts], ['custom-js']);
    gulp.watch([paths.styles], ['custom-css']);
    gulp.watch([paths.templates], ['custom-templates']);
    gulp.watch([paths.lib]);
    gulp.watch([paths.index], ['usemin-index']);
    gulp.watch([paths.login], ['usemin-login']);
});

gulp.task('webserver', function () {
    connect.server({
        root: 'dist',
        livereload: true,
        port: 8888
    });
});

gulp.task('livereload', function () {
    gulp.src(['dist/**/*.*'])
        .pipe(watch(['dist/**/*.*']))
        .pipe(connect.reload());
});
/********************** live reload end **********************/

gulp.task('build', ['usemin', 'build-assets', 'build-custom']);

// 自动构建
gulp.task('bomb', ['build', 'webserver', 'livereload', 'watch']);

// 清空dist下的内容
gulp.task('clean', function () {
    gulp.src('dist/*', {read: false})
        .pipe(clean());
});